package edu.chen.controller;

import edu.chen.exception.lease.NotFountException;
import edu.chen.exception.login.NoUserException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

@ControllerAdvice   //该注解用于声明一个适用于全局的异常处理类
public class LibraryExceptionHandler {
     static final String DEFAULT_ERROR_VIEW = "error";

    /**
     * 错误处理注解
     * 注解的方法用户处理当前Controller中抛出的对应异常.
     *              如果没有ControllerAdvices标注,
     *              则只会在该方法在的Controller内生效 处理该Controller内产生的异常
     * @param request
     * @param ex
     * @return
     */
    @ExceptionHandler(value = NoUserException.class)
    public ModelAndView defaultHandler(HttpServletRequest request,Exception ex) {
        System.out.println("HelloWorld");
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.getModel().put("message","NoUserException");
        modelAndView.setViewName(DEFAULT_ERROR_VIEW);
        return modelAndView;
    }

    @ExceptionHandler(value = NotFountException.class)
    public ModelAndView NotFound(Exception ex)   {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.getModel().put("message","NotFoundException");
        modelAndView.setViewName(DEFAULT_ERROR_VIEW);
        return modelAndView;
    }
}
